<?php

class urls {
	/**
	 * Takes an URL and splits it up into it's protocol, site, and resource parts
	 * 
	 * Returns an associative array of protocol, port, site, and resource parts
	 * Note: the URL does not have to have a protocol specified 
	 * example:
	 * <code>
	 *     $url = "http://www.foo.com/blog/search?q=bar";
	 *     $url_parts = getUrlParts($url);
	 *     // $url_parts will contain the following:
	 *     // Array
	 *     // (
	 *     //     [protocol] => http
	 *     //     [port] =>
	 *     //     [site] => www.foo.com
	 *     //     [resource] => blog/search?q=bar
	 *     // )
	 *     
	 *     $url = "www.foo.com:8080/blog/search?q=bar";
	 *     $url_parts = getUrlParts($url);
	 *     // $url_parts will contain the following:
	 *     // Array
	 *     // (
	 *     //     [protocol] =>
	 *     //     [port] => 8080
	 *     //     [site] => www.foo.com
	 *     //     [resource] => blog/search?q=bar
	 *     // )
	 * </code>
	 * 
	 * @param string	The URL that you want to split up
	 * @return associative array 	Array containing the split up parts of the URL
	 */
	function getUrlParts($url) {
		$result = array();
		
		// Get the protocol, site and resource parts of the URL
		// original url = http://example.com/blog/index?name=foo
		// protocol = http://
		// site = example.com/
		// resource = blog/index?name=foo
		$regex = '#^(.*?//)*([\w\.\d]*)(:(\d+))*(/*)(.*)$#';
		$matches = array();
		preg_match($regex, $url, $matches);
				
		// Assign the matched parts of url to the result array
		$result['protocol'] = $matches[1];
		$result['port'] = $matches[4];
		$result['site'] = $matches[2];
		$result['resource'] = $matches[6];
		
		// clean up the site portion by removing the trailing /
		$result['site'] = preg_replace('#/$#', '', $result['site']);
		
		// clean up the protocol portion by removing the trailing ://
		$result['protocol'] = preg_replace('#://$#', '', $result['protocol']);
		
		return $result;
	}
	
	public static function getURL() {
		return $_REQUEST;
	}
	
	public static function getLang() {
		$url = urls::getURL();
		
		
		$lang = substr($url['q'], 0, 2);
		
		return $lang;
	}
	
	public static function getLangNum() {
		$langs = array('fr' => '2', 'de' => '1');
		
		$langStr = urls::getLang();
		
		$result = 2;
		
		if (!empty($langStr)) {
		
		
			$result = $langs[$langStr];
		} else {
		
			// French per default
			
			$browserLang = self::getPreferredLanguage();
			
			
			$result = $langs[$browserLang];
			
		}
		
		return $result;
		
	}
	
	
	private function getPreferredLanguage() {
		$langs = array();
		if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
			// break up string into pieces (languages and q factors)
			preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
					$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
			if (count($lang_parse[1])) {
				// create a list like "en" => 0.8
				$langs = array_combine($lang_parse[1], $lang_parse[4]);
				// set default to 1 for any without q factor
				foreach ($langs as $lang => $val) {
					if ($val === '') $langs[$lang] = 1;
				}
				// sort list based on value	
				arsort($langs, SORT_NUMERIC);
			}
		}
		//extract most important (first)
		foreach ($langs as $lang => $val) { break; }
		//if complex language simplify it
		if (stristr($lang,"-")) {$tmp = explode("-",$lang); $lang = $tmp[0]; }
		return $lang;
	}	
}
?>